/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.atomojo.app.storage.file;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.atomojo.app.db.Entry;
import org.infoset.xml.Attribute;
import org.infoset.xml.DocumentLoader;
import org.infoset.xml.Element;
import org.infoset.xml.InfosetFactory;
import org.infoset.xml.Item;
import org.infoset.xml.ItemConstructor;
import org.infoset.xml.ItemDestination;
import org.infoset.xml.XMLException;
import org.infoset.xml.filter.ItemFilter;
import org.infoset.xml.filter.RemoveDocumentFilter;

/**
 *
 * @author alex
 */
public class FeedLoader {
   
   DocumentLoader loader;
   File dir;
   Logger log;
   Iterator<Entry> entries;
   public FeedLoader(Logger log,DocumentLoader loader,File dir,Iterator<Entry> entries)
   {
      this.log = log;
      this.dir = dir;
      this.loader = loader;
      this.entries = entries;
   }
   
   public void load(final ItemDestination dest)
      throws IOException,XMLException
   {
      File feedRef = new File(dir,FileStorage.FEED_DOCUMENT_NAME);
      final URI baseURI = feedRef.toURI();
      loader.generate(baseURI, new ItemDestination() {
         ItemConstructor constructor = InfosetFactory.getDefaultInfoset().createItemConstructor();
         int level = 0;
         public void send(Item item)
            throws XMLException
         {
            switch (item.getType()) {
               case ElementItem:
                  level++;
                  break;
               case ElementEndItem:
                  level--;
                  if (level==0) {
                     while (entries.hasNext()) {
                        Entry entry = entries.next();
                        File file = new File(dir,"."+entry.getUUID()+".atom");
                        try {
                           dest.send(constructor.createCharacters("\n"));
                           loader.generate(file.toURI(), 
                              new RemoveDocumentFilter(
                                 new ItemFilter() {
                                    boolean started = false;
                                    public void send(Item item) 
                                       throws XMLException
                                    {
                                       if (!started && item.getType()==Item.ItemType.ElementItem) {
                                          Element top = (Element)item;
                                          top.setBaseURI(baseURI);
                                          top.getAttributes().remove(Attribute.XML_BASE);
                                       }
                                       dest.send(item);
                                    }
                                    public void attach(ItemDestination dest) {
                                    }
                                 }
                              )
                           );
                        } catch (Exception ex) {
                           log.log(Level.SEVERE,"Cannot open entry document "+file.toURI()+" due to exception.",ex);
                        }
                        
                     }
                     /*
                     dir.listFiles(new FileFilter() {
                        public boolean accept(File file) {
                           String name = file.getName();
                           if (name.charAt(0)=='.' && !name.startsWith(".feed") && name.endsWith(".atom")) {
                              try {
                                 dest.send(constructor.createCharacters("\n"));
                                 loader.generate(file.toURI(), 
                                    new RemoveDocumentFilter(
                                       new ItemFilter() {
                                          boolean started = false;
                                          public void send(Item item) 
                                             throws XMLException
                                          {
                                             if (!started && item.getType()==Item.ItemType.ElementItem) {
                                                ((Element)item).setBaseURI(baseURI);
                                             }
                                             dest.send(item);
                                          }
                                          public void attach(ItemDestination dest) {
                                          }
                                       }
                                    )
                                 );
                              } catch (Exception ex) {
                                 log.log(Level.SEVERE,"Cannot open entry document "+file.toURI()+" due to exception.",ex);
                              }
                           }
                           return false;
                        }
                     });
                      */
                     dest.send(constructor.createCharacters("\n"));
                  }
                  break;
            }
            dest.send(item);
         }
      });
   }

}
